<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator=">" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">系统管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">系统日志</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content">
      <div class="flex-bet">
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="">
            <el-cascader v-if="userType == 0" v-model="searchForm.companyId" placeholder="请选择公司名称" :options="companyArray" :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'childList',
              }" @change="changeTreeItem" clearable></el-cascader>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.type" placeholder="请选择操作类型" @change="onSubmit" clearable>
              <el-option label="PC" value="0"></el-option>
              <el-option label="大屏" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-input show-word-limit placeholder="请输入操作标题" v-model="searchForm.title" clearable></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-input show-word-limit placeholder="请输入操作人" v-model="searchForm.username" clearable></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-date-picker v-model="date" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd" @change="handleDate" clearable>
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <div v-allow="'010401'" class="search-btn" @click="onSubmit"></div>
          </el-form-item>
          <el-form-item>
            <div v-allow="'010401'" class="reset-btn" @click="resetForm"></div>
          </el-form-item>
        </el-form>
      </div>
      <NxTable :table-cols="tableCols" :table-data="tableData" :pag-ination="pagination" class="table-wrap" @refresh="refresh">
        <template slot="type" slot-scope="scope">
          <span v-if="scope.row.type == 0">后台管理</span>
          <span v-if="scope.row.type == 1">可视化大屏</span>
        </template>
      </NxTable>
    </div>
  </div>
</template>
<script>
import NxTable from '@/components/NxTable.vue'
import { Page } from '@/utils/pagination'
import { getSysLogList } from '@/api/system'
import { getCompanyInfoTree } from '@/api/company'
import mixin from '@/api/CompanyInfoTree.js'
export default {
  mixins: [mixin],
  components: {
    NxTable
  },
  // 表格数据
  data() {
    return {
      userType: '',
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        companyId: '',
        title: '',
        startDate: '',
        endDate: '',
        username: ''
      },
      // 表格字段
      tableCols: [
        {
          label: '公司名称',
          prop: 'companyName',
          align: 'center'
        },
        {
          label: '操作类型',
          slot: 'type',
          align: 'center'
        },
        {
          label: '操作标题',
          prop: 'title',
          align: 'center'
        },
        {
          label: '操作内容',
          prop: 'content',
          align: 'center'
        },
        {
          label: '操作人',
          prop: 'username',
          align: 'center'
        },
        {
          label: '操作时间',
          prop: 'createDate',
          align: 'center'
        }
      ],
      tableData: [],
      pagination: new Page(),
      date: [], // 日期范围
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      companyArray: [] // 公司集合
    }
  },
  // 初始化加载
  mounted() {
    // // 查询
    this.getList()
    this.userType = Number(window.localStorage.getItem('userType'))

    // // 获取公司信息树形列表
    this.getCompanyInfoTree()
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1
      this.getList()
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum
      this.searchForm.pageSize = val.pageSize
      this.getList()
    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        title: '',
        startDate: '',
        endDate: ''
      }
      this.date = []
      this.onSubmit()
    },
    // 查询列表
    getList() {
      getSysLogList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.data
          this.pagination.count = res.data.total
        }
      })
    },
    // 日期切换
    handleDate() {
      if (this.date && this.date.length == 2) {
        this.searchForm.startDate = this.date[0]
        this.searchForm.endDate = this.date[1]
      } else {
        this.searchForm.startDate = ''
        this.searchForm.endDate = ''
      }

      this.getList()
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.searchForm.companyId = val[val.length - 1]
      } else {
        this.searchForm.companyId = ''
      }

      // 查询
      this.getList()
    }
    // 获取公司列表
    // getCompanyInfoTree() {
    //   getCompanyInfoTree({}).then((res) => {
    //     if (res.code == 200) {
    //       this.companyArray = res.data
    //     }
    //   })
    // },
  }
}
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;
  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;
    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }
    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>
